<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MX_Controller {
	protected $data;
	function __construct() {
        parent::__construct();
        $this->data = new stdClass();
        $this->template->set_template('admin');
        //CHECK LANGUAGE
        $this->load->library('session');
        if (!$this->session->userdata('lang_default')) {
            $this->load->model('language_model', 'language');
            $list_language = $this->language->get_language();
            if(!empty($list_language))
            {
                $lang_default = $list_language[0];
                $this->session->set_userdata('lang_default', $lang_default);
                
                if (!$this->session->userdata('lang_current')) {
                    $this->session->set_userdata('lang_current', $lang_default);
                }
            }
        }
        $this->load->model('language_model', 'language');
        $this->load->model('category_model', 'category');
        $this->load->model('link_model', 'link');
    }
	
    /* *****************************************************************************************************************
     * Name         : index
     * -----------------------------------------------------------------------------------------------------------------
     * Description  :
     * -----------------------------------------------------------------------------------------------------------------
     * Params       :
     * -----------------------------------------------------------------------------------------------------------------
     * Return       :
     * -----------------------------------------------------------------------------------------------------------------
     * Warning      :
     * -----------------------------------------------------------------------------------------------------------------
     * Copyright    : TAOWEBSITE
     * -----------------------------------------------------------------------------------------------------------------
     * Created      : New  2014/06/15 (TienPham)  
     * *************************************************************************************************************** */
     
	public function index()
	{
        $all_category = $this->category->get_all_category();
        $all_category = $all_category['current'];

        $list_category = $this->category->list_category($all_category);
        $this->data->list_category = $list_category;

	   $this->template->write_view('content', 'category/list', $this->data, TRUE);
       $this->template->render();

	}
    public function add()
	{
	   $this->data->language = $this->language->get_language();
       $this->data->sort_order = $this->category->max_sort_order();
       
       $all_category = $this->category->get_all_category();
       $all_category = $all_category['current'];
       $list_category = $this->category->list_category($all_category);
       $this->data->list_category = $list_category;
       
       $this->template->write_view('content', 'category/form_add', $this->data, TRUE);
       $this->template->render();
       if(isset($_POST['btn_submit']))
       {
           $this->insert();
       }
	}
    
    public function edit($category_id = "")
	{
	   $this->data->language = $this->language->get_language();
       $category_info = $this->category->get_category_by_id($category_id);
       $category_image = $this->category->get_image_category_by_id($category_id);
       $category = $category_info['category'];
       if(empty($category))
       {
            $link = admin_url() . 'category';
            redirect($link);
       }
       $category_desc = $category_info['category_desc'];
       $link = $category_info['link'];
       $this->data->category = $category;
       $this->data->category_desc = $category_desc;
       $this->data->link = $link;
       $this->data->category_image = $category_image;
       
       $all_category = $this->category->get_all_category();
       $all_category = $all_category['current'];
       $list_category = $this->category->list_category($all_category);
       $this->data->list_category = $list_category;

       $this->template->write_view('content', 'category/form_edit', $this->data, TRUE);
       $this->template->render();
       if(isset($_POST['btn_submit']))
       {
           $this->update($category_id);
       }
	}
    
    public function insert()
    {
        $language = $this->language->get_language();
        $data = array();
        $data['category'] = array(
                'parent_id' => $_POST['category'],
                'date_added' => time(),
                'date_modified' => time(),
                'status' => $_POST['status'],
                'sort_order' => $_POST['sort_order'],
                'other' => $_POST['other']
        );
        foreach($language as $lang)
        {
            $lang_code = $lang['lang_code'];
            $data['category_desc'][$lang_code] = array(
                    'title' => $_POST["title_$lang_code"],
                    'description' => $_POST["description_$lang_code"],
                    'long_description' => $_POST["long_description_$lang_code"],
                    'meta_title' => $_POST["meta_title_$lang_code"],
                    'meta_description' => $_POST["meta_description_$lang_code"],
                    'meta_keywords' => $_POST["meta_keywords_$lang_code"]
            );
            
            $data['link'][$lang_code] = array(
                'link' => $_POST["link_$lang_code"]
            );
        }
        
        $main_image = $_POST['main_image'];
        $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
        $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
        $data['image']['main_image'] = $main_image;
        $data['image']['other_image'] = $other_image;
        $data['image']['order_image'] = $order_image;
        
        $this->category->insert($data);
    }
    
    public function update($category_id)
    {
        $language = $this->language->get_language();
        $data = array();
        $data['category'] = array(
                'parent_id' => $_POST['category'],
                'date_added' => time(),
                'date_modified' => time(),
                'status' => $_POST['status'],
                'sort_order' => $_POST['sort_order'],
                'other' => $_POST['other']
        );
        foreach($language as $lang)
        {
            $lang_code = $lang['lang_code'];
            $data['category_desc'][$lang_code] = array(
                    'title' => $_POST["title_$lang_code"],
                    'description' => $_POST["description_$lang_code"],
                    'long_description' => $_POST["long_description_$lang_code"],
                    'meta_title' => $_POST["meta_title_$lang_code"],
                    'meta_description' => $_POST["meta_description_$lang_code"],
                    'meta_keywords' => $_POST["meta_keywords_$lang_code"]
            );
            
            $data['link'][$lang_code] = array(
                'link' => $_POST["link_$lang_code"]
            );
        }
        
        $main_image = $_POST['main_image'];
        $other_image = isset($_POST['other_image']) ? $_POST['other_image'] : array();
        $order_image = isset($_POST['order_image']) ? $_POST['order_image'] : array();
        $data['image']['main_image'] = $main_image;
        $data['image']['other_image'] = $other_image;
        $data['image']['order_image'] = $order_image;
        
        $this->category->update($category_id, $data);
    }
    
    public function change_status()
    {
       $category_id = $_POST['category_id'];
       
       $result = array();
       
       $status = $this->category->change_status($category_id);
       $result['status'] = $status;
       $result['html'] = $status == 0 ? trans2('disable') : trans2('enable');
       echo json_encode($result);
    }
    
    public function delete_category()
    {
         $category_id = $_POST['category_id'];
         $result = $this->category->delete_category($category_id);
    }
}